import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/30 12:19
 * 说明：客户端发送消息给服务器端，服务器端在控制台输出
 */

public class TCPTest {
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            // 1. 创建Socket，声明对方（服务器端）的ip地址和端口号
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            int port = 8989;
            socket = new Socket(inetAddress, port);

            // 2. 发送数据
            os = socket.getOutputStream();
            os.write("你好，我是abc123".getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 3. 关闭Socket、关闭流
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Test
    public void server() {
        Socket socket = null;
        ServerSocket serverSocket = null;
        InputStream is = null;
        try {
            // 1. 创建ServerSocket
            int port = 8989;
            serverSocket = new ServerSocket(port);
            // 2. accept()接收客户端的Socket
            socket = serverSocket.accept();
            System.out.println("Server online");
            System.out.println("Connect from: " + socket.getInetAddress().getHostAddress());

            // 3. 接收数据
            is = socket.getInputStream();
            byte[] buffer = new byte[5];
            int len;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();//内部维护了一个byte[]
            while ((len = is.read(buffer)) != -1) {
                // 可能导致乱码
//                String str = new String(buffer, 0, len);
//                System.out.print(str);
                //
                baos.write(buffer, 0, len);
            }

            System.out.println(baos.toString());
            System.out.println("\n 数据接收完成");

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (serverSocket != null) {
                    serverSocket.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
